<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>知云——针对编程的个性化学习推荐系统</title>
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <base href="${APP_PATH}">
    <link rel="shortcut icon" type="image/x-icon" href="${APP_PATH}/static/favicon.ico"/>
    <link rel="stylesheet" href="${APP_PATH}/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/toastr.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/offcanvas.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/fontawesome.min.css">


</head>
<body>

<jsp:include page="${APP_PATH}/common/head-mine.jsp"/>
<div class="col-sm-12 col-md-12 col-lg-9" style="margin-left: auto;margin-right: auto;">
    <div class="my-2 p-3 bg-white rounded d-flex">
        <div class="flex-fill text-center"><a class="text-dark" href="/user-message">公告</a></div>
        <div class="flex-fill text-center"><a class="text-danger" href="#">博客</a></div>
        <div class="flex-fill text-center"><a class="text-dark" href="/user-message-star">关注</a></div>
        <div class="flex-fill text-center"><a class="text-dark" href="/user-message-question">问题</a></div>
        <div class="flex-fill text-center"><a class="text-dark" href="/user-message-answer">回答</a></div>
    </div>
    <div class="my-2 py-5 px-4 bg-white rounded" style="position: relative;">
        <a id="readAll" style="position:absolute;top:2px;right:4px;" onclick="readAll()" class="btn btn-sm btn-danger text-white">全部已读</a>
        <div id="items">

        </div>
    </div>
    <div id="pagenav"></div>
</div>

<div class="p-4"></div>
<script src="${APP_PATH}/static/js/jquery-3.3.1.min.js"></script>
<script src="${APP_PATH}/static/js/bootstrap.min.js"></script>
<script src="${APP_PATH}/static/js/toastr.min.js"></script>
<script src="${APP_PATH}/static/js/offcanvas.js"></script>

<script>
    function read(ele) {
        var userMessageId = $(ele).attr("data-userMessageId");
        $.ajax({
            url:'/messages/read/'+userMessageId,
            type:'GET',
            success:function (result) {
                if (result.code === 100) {
                    $("a[data-flag-userMessageId='" + userMessageId + "']").remove();
                }else{
                    toastr.error("操作失败！");
                }
            }
        });
    }

    function readAll() {
        $.ajax({
            url:'/messages/read/type/1',
            type:'GET',
            success:function (result) {
                if (result.code === 100) {
                    window.location.reload(true);
                }else{
                    toastr.error("操作失败");
                }
            }
        });
    }

    function buildPage(result) {
        var $items = $("#items");
        $items.empty();
        $.each(result.extend.pageInfo.list,function (index,item) {
            if (item) {
                var $div_out = $("<div class='pt-3 border-top border-dark'></div>");
                var $p = $("<div></div>");
                var $strong = $("<strong><a class='text-dark' href='/user/"+item.fromuserid+"'>"+item.fromusername+"</a></strong>");
                var $content=$("<a href='/blog/"+item.messagetypeid+"' class='text-dark'>"+item.messagecontent+"</a>");
                var $time = $("<div class='small text-muted'>"+new Date(item.edittime).toLocaleString()+"</div>");
                //开始封装
                $div_out.append($p.append($strong).append($content).append($time));
                if (item.hasread === 0) {
                    var $div_small = $("<a data-flag-userMessageId='"+item.id+"' style='cursor: pointer;' onclick='read(this)' data-userMessageId='"+item.id+"' class='float-right'>标为已读</a>");
                    $time.append($div_small);
                }
                $div_out.appendTo($items);
            }
        })
    }

    function buildPage_pagenav(result) {
        var pagenav = $("#pagenav");
        pagenav.empty();

        var div = $("<div class='p-2'>");
        var ul = $("<ul></ul>").addClass("pagination");
        //遍历给ul中添加页码提示
        $.each(result.extend.pageInfo.navigatepageNums,function(index,item){

            var numLi = $("<li class='page-item'></li>").append($("<a class='btn btn-sm btn-outline-primary mr-2'></a>").append(item));
            if(result.extend.pageInfo.pageNum === item){
                //当前页按钮，不可点击
                numLi.find("a").removeClass("btn-outline-primary");
                numLi.find("a").addClass("disabled");
                numLi.find("a").addClass("btn-primary");
            }
            numLi.click(function(){
                toPage_blogs(item);
            });
            ul.append(numLi);
        });
        div.append(ul).appendTo(pagenav);
    }

    function toPage(pn) {
        $.ajax({
            url: '/messages/1/'+pn,
            type: 'GET',
            success: function (result) {
                if (result.code === 100) {
                    if (result.extend.pageInfo.list.length > 0) {
                        buildPage(result);
                        buildPage_pagenav(result);
                    }else{
                        toastr.info("暂无该类消息！");
                    }
                }else{
                    toastr.error(result.extend.error);
                }
            }
        })
    }

    $(function (result) {
        toPage(1);
    });
</script>

</body>

</html>
